import org.jlinalg.LinSysSolver;
import org.jlinalg.Matrix;
import org.jlinalg.Rational;
import org.jlinalg.Vector;

import java.util.*;
import java.io.*;
import java.math.*;

/**
 * Example that computes a solution of a linear equation system for the domain
 * of rational numbers.
 * 
 * @author Andreas Keilhauer, Georg Thimm
 */
public class Null2
{
	/**
	 * start the demonstration
	 * 
	 * @param argv
	 *            is ignored
	 */
	public static void main(String[] argv)
	{
	String fileName = argv[0];
	String rows = argv[1];
	String cols = argv[2];

	int r = Integer.parseInt(rows);
	int c = Integer.parseInt(cols);

	System.out.println("// Will assume that the file '"+fileName+"' contains "+r+" rows and "+c+" columns.");

	Rational[][] nums = new Rational[r][c];
	
	int at = 0;

                        try     {
                                FileReader fr = new FileReader(fileName);
                                BufferedReader br = new BufferedReader(fr);

                                String record = new String();
                                while((record=br.readLine()) != null)
                                        {
                                        if( record.startsWith("//") ) continue; //! ignore comments

					System.out.println("// Read: "+record);

                                        String[] tripData = record.split(" ");

					System.out.print("// Save:");

					for( int x=0; x<c; x++ )
						{
						String ratString = tripData[x];

                                                int locbar = ratString.indexOf('/');
                                                if( locbar == -1 )
                                                        {
							//! No '/' symbol? Just read it in as a single numnbber
							double d = Double.parseDouble(tripData[x]);

							nums[at][x] = new Rational(new Double(d));
                                                        }
                                                else
                                                        {

							String numerator = null;
							String denominator = null;

                                                        numerator = ratString.substring( 0, locbar );
                                                        denominator = ratString.substring( locbar+1 );

	                                                BigInteger bnum = new BigInteger(numerator);
 	                                                BigInteger dnum = new BigInteger(denominator);

							nums[at][x] = new Rational(bnum,dnum);
                                                        }


						System.out.print(" "+nums[at][x]);

						}
					at++;
					System.out.println();
					}

                                }
                        catch (IOException e)
                                {
                                // catch possible io errors from readLine()
                                System.out.println("Problem reading file "+fileName);
                                System.exit(0);
                                }


		 Matrix<Rational> a = new Matrix<Rational>(nums);

		 System.out.println("// Matrix object has "+a.getRows()+" rows and "+a.getCols()+" columns.");
		
		Rational zero[] = new Rational[a.getRows()];
		for(int x=0; x<zero.length; x++ )
			{
			zero[x] = Rational.FACTORY.get(0);
			}

		Vector<Rational> b = new Vector<Rational>(zero);

		// calculate the solution space and print it

		// Object ls = LinSysSolver.solutionSpace(a,b);

		// System.out.println("Null space has dimension "+(((AffineLinearSubspace<Rational[]>)ls).getDimension()));

		System.out.println(LinSysSolver.solutionSpace(a, b).toString());

		//! System.out.println("x = " + solution);

	}
}

